package com.book.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.book.bean.OrderInfo;
import com.book.bean.OrderListInfo;
import com.book.bean.Users;
import com.book.biz.OrderInfoBiz;
import com.book.biz.OrderInfoListBiz;
import com.book.biz.impl.OrderInfoBizImpl;
import com.book.biz.impl.OrderInfoListBizImpl;
import com.book.dao.impl.UserDaoImpl;


public class SelectOrderInfoServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		
		String uname = (String)request.getSession().getAttribute("uname");
		if(uname==null){
			request.getRequestDispatcher("/login_user.jsp").forward(request, response);
		}else{
			String type = request.getParameter("type");
			if("orderInfo".equals(type)){
				OrderInfoBiz oBiz = new OrderInfoBizImpl();
				try {
					Users user = new UserDaoImpl().getUsers(uname);
					List<OrderInfo> olist =oBiz.getOrderByUser(user.getUid());
					session.setAttribute("olist", olist);
					request.getRequestDispatcher("order.jsp").forward(request, response);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			if("orderList".equals(type)){
				int orderid = Integer.parseInt(request.getParameter("orderid"));
				OrderInfoListBiz oiBiz = new OrderInfoListBizImpl();
				List<OrderListInfo> oInfo =oiBiz.selectOrderListInfo(orderid);
				session.setAttribute("orderid", orderid);
				request.getSession().setAttribute("oInfo", oInfo);
				request.getRequestDispatcher("orderInfo.jsp").forward(request, response);
			}
		}
		
	}

}
